/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.bean.model;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import javax.swing.AbstractListModel;

/**
 *
 * @author yura
 */
public class DoubleListModel {
    private final List<String> availableGroup;
    private final List<String> selectedGroup;
    private final GUIListModel availableModel;
    private final GUIListModel selectedModel;

    /**
     *  Create new instance with empty, but not null,
     * available and selected lists
     */
    public DoubleListModel() {
        this(null, null);
    }

    /**
     *  Create new instance
     * @param availableGroup available group list, if it null
     *      create new LinkedList
     * @param selectedGroup  selected group list, if it null
     *      create new LinkedList
     */
    public DoubleListModel(List<String> availableGroup, List<String> selectedGroup) {
        if (availableGroup == null){
            availableGroup = new LinkedList<String>();
        }
        if (selectedGroup == null){
            selectedGroup = new LinkedList<String>();
        }
        this.availableGroup = availableGroup;
        this.selectedGroup = selectedGroup;
        availableModel = new GUIListModel(this.availableGroup);
        selectedModel = new GUIListModel(this.selectedGroup);
    }

    /**
     *  Return <b>pointer</b> to list with available data
     * @return list with available data
     */
    public List<String> getAvailableGroup() {
        return availableGroup;
    }

    /**
     *  Data not replaced, from collection with available
     * data, removed all elements and aded all elements from
     * availableGroup
     * @param availableGroup new elements
     */
    public void setAvailableGroup(Collection<String> availableGroup) {
        this.availableModel.removeAllElements();
        this.availableModel.addAllElements(availableGroup);
    }

    /**
     *  Return <b>pointer</b> to list with selected data
     * @return list with selected data
     */
    public List<String> getSelectedGroup() {
        return selectedGroup;
    }

    /**
     *  Data not replaced, from collection with selected
     * data, removed all elements and aded all elements from
     * selectedGroup
     * @param selectedGroup new elements
     */
    public void setSelectedGroup(Collection<String> selectedGroup) {
        this.selectedModel.removeAllElements();
        this.selectedModel.addAllElements(selectedGroup);
    }

    public GUIListModel getAvailableGroupListModel(){
        return availableModel;
    }

    public GUIListModel getSelectedGroupListModel(){
        return selectedModel;
    }

    public static class GUIListModel extends AbstractListModel{
        private final List<String> data;

        public GUIListModel(List<String> data) {
            this.data = data;
        }

        @Override
        public int getSize() {
            return data.size();
        }

        @Override
        public String getElementAt(int index) {
            return data.get(index);
        }

        public void addElement(String obj) {
            data.add(obj);
            fireIntervalAdded(this, data.size() - 1, data.size() - 1);
        }

        public String removeElement(int index) {
            String remove = data.remove(index);
            fireIntervalRemoved(this, index, index);
            return remove;
        }

        public boolean removeElement(String obj) {
            int index = data.indexOf(obj);
            boolean remove = data.remove(obj);

            if (index != -1) {
                fireIntervalRemoved(this, index, index);
            }
            
            return remove;
        }

        public void removeAllElements() {
            int size = getSize();
            if (size > 0) {
                data.clear();
                fireIntervalRemoved(this, 0, size - 1);
            }
        }

        public void addAllElements(Collection<String> elements){
            int size = this.data.size();
            if (elements != null && ! elements.isEmpty()) {
                this.data.addAll(elements);
                fireIntervalAdded(this, size, this.data.size() - 1);
            }
        }
    }
}
